/*
 * Copyright (c) 2017. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.activity.delivery.order.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.EmojiFilter;
import cn.com.dhc.danlu.util.ToastUtil;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;

/**
 * author: wuhaiyang(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-02 下午4:06<br/>
 *
 * <p>
 * 取消原因弹窗
 * </p>
 */
public class CancelReasonPopupView extends BasePopupWindow {
    private RadioGroup rgContainer;
    private AppCompatEditText etReason;
    private TextView tvCountLimit;
    private ICancelOrderListener listener;

    public CancelReasonPopupView(Context context, ICancelOrderListener listener) {
        super(context);
        this.listener = listener;
        init(context);
    }

    private void init(Context context) {
        View contentView = View.inflate(context, R.layout.popup_cancel_order_reason, null);
        rgContainer = (RadioGroup) contentView.findViewById(R.id.rg_container);
        etReason = (AppCompatEditText) contentView.findViewById(R.id.et_cancel_reason);
        tvCountLimit = (TextView) contentView.findViewById(R.id.tv_count_limit);

        etReason.setFilters (new InputFilter[] {new EmojiFilter (), new InputFilter.LengthFilter (20)});

        bindAction(contentView);
        setContentView(contentView);
        setAttr();
    }

    private void bindAction(View contentView) {
        contentView.findViewById(R.id.tv_complete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                String cancelReason = getReason();
                if (TextUtils.isEmpty(cancelReason)) {
                    ToastUtil.getInstance().show("请输入取消原因");
                    return;
                }
                CommonUtils.hideSoftInputFromWindow(getContext(), etReason);
                if (null != listener) {
                    listener.onCancel(cancelReason);
                }
            }
        });
        rgContainer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                View view = (View) etReason.getParent();
                if (checkedId == R.id.acrb_other_reason) {
                    view.setVisibility(View.VISIBLE);
                    CommonUtils.showSoftInputFromWindow(getContext(), etReason);
                } else {
                    view.setVisibility(View.GONE);
                    CommonUtils.hideSoftInputFromWindow(getContext(), etReason);
                }
            }
        });
        etReason.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                tvCountLimit.setText(temp.length() + "/20");
            }
        });
    }

    private String getReason() {
        int buttonId = rgContainer.getCheckedRadioButtonId();
        if (buttonId == R.id.acrb_no_intent) {
            return getContext().getResources().getString(R.string.reason_no_intent);
        } else if (buttonId == R.id.acrb_rebuy) {
            return getContext().getResources().getString(R.string.reason_repurchase);
        } else if (buttonId == R.id.acrb_payfailure) {
            return getContext().getString(R.string.reason_payfailure);
        } else {
            String replace = etReason.getText().toString().replace(" ", "");
            if (!TextUtils.isEmpty(replace)) {
                return getContext().getString(R.string.reason_other) + "：" + replace;
            }
            return getContext().getResources().getString(R.string.reason_other);
        }
    }

    private Context getContext() {
        return rgContainer.getContext();
    }

    private void setAttr() {
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        setAnimationStyle(R.style.popwin_anim_style);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    public interface ICancelOrderListener {
        void onCancel(String reason);
    }

}
